package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.SysUserDTO;
import com.andy.blog.admin.service.SysUserBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.core.domain.entity.SysUser;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.common.utils.poi.ExcelUtil;
import com.andy.blog.dal.service.SysRoleDalService;
import com.andy.blog.dal.service.SysUserDalService;
import com.andy.blog.dal.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 用户信息
 * Created by andy on 2023-04-05 11:38
 */
@RestController
//@RequestMapping("/system/user")
public class SysUserController {
    @Autowired
    private SysUserDalService userDalService;

    @Resource
    private SysUserBusService sysUserBusService;


    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/system/user/list")
    public GeneralResponseResult<PageUtilsDTO<SysUserDTO>> list(SysUserVO sysUserVO) {
        return GeneralResponseResult.success(sysUserBusService.selectListByPage(sysUserVO));
    }

    //@Log(title = "用户管理", businessType = BusinessTypeEnum.EXPORT)
    //@PreAuthorize("@ss.hasPermi('system:user:export')")
    //@PostMapping("/export")
    //public void export(HttpServletResponse response, SysUser user) {
    //    List<SysUser> list = sysUserBusService.selectUserList(user);
    //    ExcelUtil<SysUser> util = new ExcelUtil<>(SysUser.class);
    //    util.exportExcel(response, list, "用户数据");
    //}

    @Log(title = "用户管理", businessType = BusinessTypeEnum.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/system/user/importData")
    public GeneralResponseResult<String> importData(MultipartFile file, boolean updateSupport) throws Exception {
        String message = sysUserBusService.importUser(file, updateSupport);
        return GeneralResponseResult.success(message);
    }

    /**
     * 下载模版
     */
    @PostMapping("/system/user/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysUser> util = new ExcelUtil<>(SysUser.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = {"/system/user/", "/system/user/{userId}"})
    public GeneralResponseResult<Map<String, Object>> getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        return GeneralResponseResult.success(sysUserBusService.getInfo(userId));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "用户管理", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/system/user")
    public GeneralResponseResult<String> add(@Validated @RequestBody SysUserVO sysUserVO) {
        sysUserBusService.add(sysUserVO);
        return GeneralResponseResult.success();
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/system/user")
    public GeneralResponseResult<Void> edit(@Validated @RequestBody SysUserVO sysUserVO) {
        sysUserBusService.edit(sysUserVO);
        return GeneralResponseResult.success();
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/system/user/{userId}")
    public GeneralResponseResult<Void> remove(@PathVariable Long userId) {
        sysUserBusService.remove(userId);
        return GeneralResponseResult.success();
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/system/user/resetPwd")
    public GeneralResponseResult<Void> resetPwd(@RequestBody SysUserVO sysUserVO) {
        sysUserBusService.resetPwd(sysUserVO);
        return GeneralResponseResult.success();
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/system/user/changeStatus")
    public GeneralResponseResult<Void> changeStatus(@RequestBody SysUserVO sysUserVO) {
        sysUserBusService.changeStatus(sysUserVO);
        return GeneralResponseResult.success();
    }

    /**
     * 根据用户编号获取授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping("/system/user/authRole/{userId}")
    public GeneralResponseResult<Map<String, Object>> authRole(@PathVariable("userId") Long userId) {
       return GeneralResponseResult.success(sysUserBusService.authRole(userId));
    }

    /**
     * 用户授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessTypeEnum.GRANT)
    @PutMapping("/system/user/authRole")
    public GeneralResponseResult<Void> insertAuthRole(Long userId, Long[] roleIds) {
        userDalService.insertUserAuth(userId, roleIds);
        return GeneralResponseResult.success();
    }

    /**
     * 登录用户信息
     */
    @GetMapping("/portal/user/getUserInfo")
    public GeneralResponseResult<SysUserDTO> getUserInfo() {
        return GeneralResponseResult.success(sysUserBusService.getUserInfo());
    }
}
